package com.zretc.file.controller;

import com.zretc.common.constant.WebConstant;
import com.zretc.common.entity.R;
import com.zretc.common.entity.RespResult;
import com.zretc.file.service.FileService;
import com.zretc.api.pojo.File;
import com.zretc.security.entity.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Objects;

@RestController
@RequestMapping("/file")
public class FileController {
    @Autowired
    private FileService fileService;

//    @PostMapping("/upload")
//    public RespResult upload(MultipartFile file){
//        //System.out.println("FileController------------------------------------------------------------------------------"); //test
//        try {
//            String[] fileStr = fileService.upload(0, file);
//            String url = fileStr[0];
//            //System.out.println(url); // test
//        }catch (Exception e){
//            e.printStackTrace();
//            return R.fail(WebConstant.UPLOAD_ERROR_CODE, WebConstant.ERROR_405);
//        }
//        return R.success("上传成功");
//    }

    @PostMapping("/fileDetail")
    public RespResult getFileDetail(@RequestBody File file){
        File fileInfo = fileService.getFileById(file.getResourceId());
        if(Objects.isNull(fileInfo)){
            return R.fail(404, WebConstant.ERROR_404);
        }
        else
        {
            return R.success(fileInfo);
        }
    }

    @GetMapping("/allFile")
    public RespResult getAllFile(){
        return fileService.getAllFile();
    }

    @GetMapping("/allFile/s")
    public RespResult searchAllFileByName(@RequestParam("target") String target){
        return fileService.getAllFileByName(target);
    }

    @GetMapping("/myFile")
    public RespResult getFileByUploader(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser user = (LoginUser) authentication.getPrincipal();

        return fileService.getFileByUploader(user.getUser().getUserId());
    }

    @GetMapping("/myFile/s")
    public RespResult getFileByName(@RequestParam("target") String target){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser user = (LoginUser) authentication.getPrincipal();

        System.out.println(user.getUser().getUserId()+target);
        Integer id = user.getUser().getUserId();
        return fileService.getFileByName( id, target);
    }

    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String[] upload(@RequestPart("file") MultipartFile file){
        System.out.println("upload============FileController==============================================="); //test
        try {
            String[] fileStr = fileService.upload(0, file);
            String url = fileStr[0];
            System.out.println(url); // test
            return fileStr;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    @PostMapping("/addFile")
    public RespResult addFile(MultipartFile file){
        System.out.println("FileController==============================================="); //test

        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            LoginUser user = (LoginUser) authentication.getPrincipal();

            File fileInfo = new File();
            fileInfo.setUploaderId(user.getUser().getUserId());
            //System.out.println(fileInfo); //test
            fileInfo.setResourceName(file.getOriginalFilename());
            //System.out.println(fileInfo); //test
            String[] fileStr= fileService.upload(1, file);

            String url = fileStr[0];
            String bucketName = fileStr[1];
            String randomFileName = fileStr[2];
            //System.out.println(url); // test
            return fileService.addFile(fileInfo, url, bucketName, randomFileName);
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(WebConstant.UPLOAD_ERROR_CODE, WebConstant.ERROR_405);
        }
    }

    @DeleteMapping("/remove")
    public RespResult remove(@RequestBody File file) {
        try{
            System.out.println("FileController================================================================="); // test
            System.out.println(file);
            return fileService.remove(file);
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(309, WebConstant.ERROR_309);
        }
    }
}
